﻿$(function () {
    $("#btnAdd").click(function () { add(); });   
    $("#btnSave").click(function () { save(); });
    $("#checkAll").click(function () { checkAll(this) });
    $("#upload").click(function () { upload() });

    loadTables(1);
});

function upload() {
    var fileUpload = $("#files").get(0);
    var files = fileUpload.files;
    var data = new FormData();
    for (var i = 0; i < files.length ; i++) {
        data.append(files[i].name, files[i]);
    }
    $.ajax({
        type: "POST",
        url: "/Picture/UploadFilesAjax",
        fileSuffixs: ["jpg", "png"],
        contentType: false,
        processData: false,
        data: data,
        success: function (data) {            
            alert("上传成功！");
            $("#PicPath").val(data);
        },
        error: function () {
            alert("上传失败!");
        }
    });
}



//加载列表数据
function loadTables(pageindex) {
    $("#tableBody").html("");
    $("#checkAll").prop("checked", false);
    $.ajax({
        type: "GET",
        url: "/Gzh/YWLB/GetPagedListToJson?pageindex=" + pageindex + "&_t=" + new Date().getTime(),
        success: function (data) {
            $.each(data.rows, function (i, item) {
                var tr = "<tr>";
                tr += "<td align='center'>" + item.id + "</td>";
                tr += "<td>" + item.name + "</td>";
                tr += "<td>" + "<img class='weui-media-box__thumb' src='/UploadFiles/" + item.picPath + "'>" + "</td>";
                tr += "<td>" + item.remarks + "</td>";
                tr += "<td>" + item.officeAddress + "</td>";
                tr += "<td>" + item.officeTel + "</td>";
                tr += "<td>" + item.sortIndex + "</td>";
                tr += "<td><button class='btn btn-info btn-xs' href='javascript:;' onclick='edit(\"" + item.id + "\")'><i class='fa fa-edit'></i>  </button> <button class='btn btn-danger btn-xs' href='javascript:;' onclick='deleteSingle(\"" + item.id + "\")'><i class='fa fa-trash-o'></i>  </button> </td>"
                tr += "</tr>";
                $("#tableBody").append(tr);
            })
            var elment = $("#grid_paging_part"); //分页插件的容器id
            if (data.rowCount > 0) {
                var options = { //分页插件配置项
                    bootstrapMajorVersion: 3,
                    currentPage: pageindex, //当前页
                    numberOfPages: data.rowsCount, //总数
                    totalPages: data.pageCount, //总页数
                    onPageChanged: function (event, oldPage, newPage) { //页面切换事件
                        loadTables(newPage);
                    }
                }
                elment.bootstrapPaginator(options); //分页插件初始化
            }
        }
    })
}
//全选
function checkAll(obj) {
    $(".checkboxs").each(function () {
        if (obj.checked == true) {
            $(this).prop("checked", true)

        }
        if (obj.checked == false) {
            $(this).prop("checked", false)
        }
    });
};
//新增
function add() {
    $("#Id").val(0);
    $("#Name").val("");
    $("#PicPath").val("");
    $("#Description").val("");
    $("#OfficeAddress").val("");
    $("#OfficeTel").val("");
    $("#Sort").val(0);
    $("#IsClys").prop("checked", false);

    $("#Title").text("新增");
    //弹出新增窗体
    $("#editModal").modal("show");
};
//编辑
function edit(id) {
    $.ajax({
        type: "Get",
        url: "/Gzh/YWLB/GetByIdToJson?id=" + id + "&_t=" + new Date().getTime(),
        success: function (data) {
            $("#CreatedBy").val(data.createdBy);
            $("#CreatedOn").val(data.createdOn);

            $("#Id").val(data.id);
            $("#Name").val(data.name);
            $("#PicPath").val(data.picPath);
            $("#Description").val(data.remarks);
            $("#OfficeAddress").val(data.officeAddress);
            $("#OfficeTel").val(data.officeTel);
            $("#Sort").val(data.sortIndex);
            $("#IsClys").prop("checked", data.isClys);

            $("#Title").text("编辑")
            $("#editModal").modal("show");
        }
    })
};
//保存
function save() {
    var postData = {
        "CreatedBy": $("#CreatedBy").val(),
        "CreatedOn": $("#CreatedOn").val(),

        "Id": $("#Id").val(),
        "Name": $("#Name").val(),
        "PicPath": $("#PicPath").val(),
        "Remarks": $("#Description").val(),
        "OfficeAddress": $("#OfficeAddress").val(),
        "OfficeTel": $("#OfficeTel").val(),
        "SortIndex": $("#Sort").val(),
        "IsClys": $("#IsClys").prop('checked')
    };
    $.ajax({
        type: "Post",
        url: "/Gzh/Ywlb/AddOrModify",
        data: postData,
        success: function (data) {
            if (data.result == "Success") {
                loadTables(1)
                $("#editModal").modal("hide");
            } else {
                layer.tips(data.message, "#btnSave");
            };
        }
    });
};

//删除单条数据
function deleteSingle(id) {
    layer.confirm("您确认删除选定的记录吗？", {
        btn: ["确定", "取消"]
    }, function () {
        $.ajax({
            type: "POST",
            url: "/Gzh/YWLB/DeleteById",
            data: { "id": id },
            success: function (data) {
                if (data.result == "Success") {
                    loadTables(1)
                    layer.closeAll();
                }
                else {
                    layer.alert("删除失败！");
                }
            }
        })
    });
};

